<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加分类-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../static/css/font.css">
    <link rel="stylesheet" href="../../static/css/weadmin.css">
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="weadmin-body">
    <div class="layui-tab layui-tab-card">
        <ul class="layui-tab-title">
            <li class="layui-this">基本信息</li>
            <li>编辑权限</li>
        </ul>
        <div class="layui-tab-content" style="height: 100%;">
            <div class="layui-tab-item layui-show">
                <form id="form1" class="layui-form">
                    <input type="hidden" name="id" value="{{$data.id}}"/>
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" lay-verify="required" jq-error="请输入角色名称"
                                   placeholder="请输入角色名称" autocomplete="off" class="layui-input " value="{{$data.title}}">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">排序</label>
                        <div class="layui-input-block">
                            <input type="text" name="level" lay-verify="required|number" jq-error="请输入排序"
                                   placeholder="请输入排序" autocomplete="off" class="layui-input " value="{{$data.level}}">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline">
                            <input type="radio" name="status" title="显示" value="1" <if condition="$data.status==1">checked</if> />
                            <input type="radio" name="status" title="隐藏" value="0" <if condition="$data.status==0">checked</if>/>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">角色描述</label>
                        <div class="layui-input-block">
                            <textarea placeholder="请输入内容" class="layui-textarea" name="notation">{{$data.notation}}</textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-tab-item">
                <div class="layui-btn-container">
                    <button type="button" class="layui-btn layui-btn-sm" lay-demo="getChecked">获取选中节点数据</button>
                    <button type="button" class="layui-btn layui-btn-sm" lay-demo="setChecked">勾选指定节点</button>
                    <button type="button" class="layui-btn layui-btn-sm" lay-demo="reload">重载实例</button>
                </div>
                <div id="test12" class="demo-tree-more"></div>
            </div>
        </div>
    </div>
</div>
<script src="../../lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript">
    layui.extend({
        admin: '{/}../../static/js/admin'
    });
    layui.use(['admin', 'jquery', 'form', 'layer', 'tree', 'util', 'element',], function () {
        var admin = layui.admin,
            $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            tree = layui.tree,
            util = layui.util,
            element = layui.element;
        $.ajax({
            url:'/admin/auth_group/getUserRule/id/{{$Request.get.id}}',
            method:'get',
            success:function(data){
                //基本演示
                tree.render({
                    elem: '#test12'
                    , data: data
                    , showCheckbox: true  //是否显示复选框
                    , id: 'demoId1'
                    , isJump: false //是否允许点击节点时弹出新窗口跳转
                    , click: function (obj) {
                        var data = obj.data;  //获取当前点击的节点数据
                        layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                    }
                });
            },
            error:function (data) {
                layer.msg('服务器繁忙', {icon: 5});
            }
        }) ;

        //按钮事件
        util.event('lay-demo', {
            getChecked: function (othis) {
                var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据

                layer.alert(JSON.stringify(checkedData), {shade: 0});
                console.log(checkedData);
            }
            , setChecked: function () {
                tree.setChecked('demoId1', [12, 16]); //勾选指定节点
            }
            , reload: function () {
                //重载实例
                tree.reload('demoId1', {});

            }
        });
        //监听提交
        form.on('submit(add)', function (data) {
            let send_json = data.field;
            //发异步，把数据提交给php
            var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
            function getId(checkedData,ids=''){
                for (var i = 0; i < checkedData.length; i++) {
                    ids=ids+checkedData[i].id+',';
                    if(checkedData[i].children){
                        ids= getId(checkedData[i].children,ids);
                    }
                }
                return ids;
            }
            var str=getId(checkedData);
            var rules=(str.substring(str.length-1)==',')?str.substring(0,str.length-1):str;
            send_json.rules=rules;
            let url='/admin/auth_group/edit/id/'+send_json.id;
            delete send_json.id;
            $.ajax({
                url: url,
                method: 'post',
                data: send_json,
                dataType: 'JSON',
                success: function (res) {
                    if (res.code == 0) {
                        layer.alert("编辑成功", {
                            icon: 6
                        }, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.location.reload();
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }

                },
                error: function (data) {
                    layer.msg('服务器繁忙', {icon: 5});
                }
            });

            return false;
        });

        //遍历select option
        $(document).ready(function () {
            $("#pid-select option").each(function (text) {
                var level = $(this).attr('data-level');
                var text = $(this).text();
                if (level > 1) {
                    text = "├" + text;
                    for (var i = 1; i <= level; i++) {
                        text = "　　" + text;　//js中连续显示多个空格，需要使用全角的空格
                        //console.log(i+"text:"+text);
                    }
                }
                $(this).text(text);

            });

            form.render('select'); //刷新select选择框渲染
        });

    });
</script>
</body>

</html>